<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <!-- 插入到left插槽位置 -->
        <template #left>
          <span>总记录数: {{ total }} 条</span>
        </template>
        <!-- 插入到right插槽位置 -->
        <template #right>
          <el-button type="warning" size="small">导入excel</el-button>
          <el-button type="danger" size="small">导出excel</el-button>
          <el-button type="primary" size="small" @click="showDialog = true">新增员工</el-button>
        </template>
      </page-tools>
      <el-card style="margin-top: 10px">
        <el-table
          :data="list"
          border
          :default-sort="{ prop: 'timeOfEntry', order: 'descending' }"
        >
          <el-table-column label="序号" type="index" width="50%" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" sortable />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template #default="{row}">
              {{ formatData(row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable />
          <!-- <el-table-column label="账户状态" /> -->
          <el-table-column label="操作" width="280">
            <template>
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            :current-page="page"
            :page-size="size"
            layout="prev, pager, next"
            :total="total"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
      <el-dialog
        title="添加员工"
        :visible.sync="showDialog"
        width="50%"
      >
        <addorEdit @success="hSuccess" @close="showDialog = false" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getEmployees } from '@/api/employees'
import employeesEnum from '@/constant/employees'
import addorEdit from '@/views/employees/empDialog.vue'
const hireType = employeesEnum.hireType.reduce((acc, item) => {
  acc[item.id] = item.value
  return acc
}, {})
export default {
  name: 'VueHrEmployees',
  components: {
    addorEdit
  },
  data() {
    return {
      list: [], // 员工列表
      page: 1, // 页码值
      size: 10, // 每页的数量
      total: 0,
      showDialog: false // 总条数
    }
  },

  created() {
    this.loadEmployees()
  },

  methods: {
    hSuccess() {
      // 添加数据到最大的一页
      this.total++
      // 计算最大的页码值
      const maxPage = Math.ceil(this.total / this.size)
      this.page = maxPage
      // 重新渲染
      this.loadEmployees()
      // 关闭弹窗
      this.showDialog = false
    },
    // 格式数据的函数
    formatData(id) {
      return hireType[id] || '未知'
    },
    // 获取员工列表
    async loadEmployees() {
      try {
        // 发送请求
        const res = await getEmployees({ page: this.page, size: this.size })
        // console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        // 成功
        this.$message.success(res.message)
        // 赋值
        this.list = res.data.rows
        this.total = res.data.total
      } catch (error) {
        this.$message.error(error)
      }
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.page = val
      // 重新渲染
      this.loadEmployees()
    }
  }
}
</script>
